<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/decorators/taglibs.jsp"%>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
	<!-- 注册 -->
	<div id="signup" class="modal hide fade">
		<form onSubmit="return false;">
		    <div class="modal-header">
	              <a class="close" data-dismiss="modal">×</a>
	              <h3>注册</h3>
	        </div>
			<div class="modal-body">
				<div class="form-group">
				<label for="account">账号</label>
				<input type="text" class="form-control" id="username_signup" placeholder="请输入账号">
				</div>
				<div class="form-group">
				<label for="email">邮箱</label>
				<input type="email" class="form-control" id="email_signup" placeholder="请输入邮箱">
				</div>
				<div class="form-group">
				<label for="signup-password">密码</label>
				<input type="password" class="form-control" id="password_signup" placeholder="请输入密码">
				</div>
			</div>
	        <div class="modal-footer">
				<input type="button" class="btn btn-success" value="注册" id="signup_btn"/>
	        </div>
		</form>	        
    </div>
 	<!-- 注册结束 -->
	<!-- 导航 -->
	<div class="navbar navbar-inverse navbar-fixed-top">
		<div class="container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
					<span class="icon-bar"></span> 
					<span class="icon-bar"></span> 
					<span class="icon-bar"></span>
				</button>
			</div>
			<div class="navbar-collapse collapse">
				<ul class="nav navbar-nav">
					<li class="active"><a href="${ctx }/#">主页</a></li>
					<li><a href="${ctx }/#applications">应用</a></li>
					<li><a href="${ctx }/#photograph">摄影</a></li>
					<li><a href="${ctx }/#" id="chat-room">聊天室</a></li>
					<li><a href="${ctx }/#about">关于</a></li>
					<li class="dropdown">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown">联系<b class="caret"></b></a>
						<ul class="dropdown-menu">
							<li><a href="http://weibo.com/318280005" target="_blank">新浪微博</a></li>
							<li><a href="http://t.qq.com/superstar_chen" target="_blank">腾讯微博</a></li>
							<li><a href="#">微信</a></li>
							<li class="divider"></li>
							<li><a href="https://www.facebook.com/haixiang.chen" target="_blank">Facebook</a></li>
							<li><a href="https://plus.google.com/u/0/107273031820370586099/" target="_blank">Google+</a></li>
						</ul>
					</li>
				</ul>
				<c:choose>
				    <c:when test="${user ==  null }">
				   		<ul class="nav navbar-nav pull-right">
							<li class="divider-vertical"></li>
							<li class="dropdown">
								<a data-toggle="dropdown" class="dropdown-toggle" href="#">登录<strong class="caret"></strong></a>
								<ul class="dropdown-menu col-md-3">
									<li><a href="${ctx }/oauth/sina">新浪微博</a></li>
									<li><a href="#">腾讯QQ</a></li>
									<li class="divider"></li>
									<li>
										<form class="navbar-form" onSubmit="return false;">
									        <label for="username">用户名：<br>
					                          <input id="username" type="text" class="form-control col-md-2">
					                        </label>
					                        <label for="signin-password">密码：<br>
					                          <input id="password" type="password" class="form-control col-md-2">
					                        </label>
									        <button id="login" class="btn btn-success">登录</button>
									    </form >
									</li>
								</ul>
							</li>
							<li><a href="#signup" data-target="#signup" data-toggle="modal">注册</a></li>
						</ul>
				    </c:when>
				    <c:otherwise>
				   		<ul class="nav navbar-nav pull-right">
							<li class="active"><a href="${ctx }/me/${user.uid}"><i class="icon-user"></i>${user.username}</a></li>
							<li><a href="${ctx }/logout">退出</a></li>
						</ul>
				    </c:otherwise>
				</c:choose>
			</div>
		</div>
	</div>
	<script>
	$("#login").click(function(){
			var username = $("#username").val();
			var password = $("#password").val();
			if(username == null || username == "" || password == null || password == ""){
				$.globalMessenger().post({
		       		  message: '用户名或密码不能为空!',
		       		  type: 'error',
		       		  hideAfter: 2,
		       		  showCloseButton: false
	     		});
				return false;	
			}else{
				$.ajax({
					type : "POST", 
					async: false, 
					url : "${ctx }/login", 
					data : {
						username:	username,
						password:	password
					},
					success : function(data)
					{
						eval("var data="+data);
						if(data.code == 1001){
							window.location.reload();
						}else{
							$.globalMessenger().post({
					       		  message: data.message,
					       		  type: 'error',
					       		  hideAfter: 2,
					       		  showCloseButton: false
				     		});
						}
						
					}
				});
			}
		});
	$("#signup_btn").click(function(){
		var username = $("#username_signup").val();
		var email = $("#email_signup").val();
		var password = $("#password_signup").val();
		if(username == null || username == "" || password == null || password == "" || email == null || email==""){
			$.globalMessenger().post({
	       		  message: '输入不能存在空项!',
	       		  type: 'error',
	       		  hideAfter: 2,
	       		  showCloseButton: false
     		});
			return false;	
		}else{
			$.ajax({
				type : "POST", 
				async: false, 
				url : "${ctx }/user/saveuser.do", 
				data : {
					username:	username,
					email	:	email,
					password:	password
				},
				success : function(data)
				{
					eval("var data="+data);
					if(data.code == 1001){
						$("#username_signup").val("");
						$("#email_signup").val("");
						$("#password_signup").val("");
						$(".close").trigger( "click" );
					}
					$.globalMessenger().post({
			       		  message: data.message,
			       		  type: 'error',
			       		  hideAfter: 2,
			       		  showCloseButton: false
		     		});
				}
			});
		}
	});
	</script>